﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace vnMcdonald.Models
{
    public class ShoppingCart
    {
        vnMcdonaldContext db = new vnMcdonaldContext();
        public bool AddCart(Product product, int Quantity)
        {
            try
            {
                List<Cart> cart;
                if (Quantity < 1)
                {
                    Quantity = 1;
                }
                if (HttpContext.Current.Session["Cart"] == null)
                {
                    cart = new List<Cart>();
                    cart.Clear();
                }
                else
                {
                    cart = (List<Cart>)HttpContext.Current.Session["Cart"];
                }
                int line = CheckCart(product.ProductId, cart);
                if (line != -1)
                {
                    cart[line].Quantity = Convert.ToInt32(cart[line].Quantity) + Quantity;
                }
                else
                {
                    cart.Add(new Cart 
                    { 
                        RecordId = cart.Count + 1,
                        ProductId = product.ProductId, 
                        Name = product.Name, 
                        Price = product.Price,
                        Image = product.Images.OrderBy(im => im.Order).Select(im => im.Src).FirstOrDefault().ToString(),
                        Quantity = Quantity 
                    });
                }
                HttpContext.Current.Session["Cart"] = cart;
                return true;
            }
            catch (Exception)
            {

                return false;
            }    
            
        }
        public static int CheckCart(int Pid, List<Cart> cart)
        {
            int line = -1;
            for (int i = 0; i < cart.Count; i++)
            {
                if (int.Parse(cart[i].ProductId.ToString()) == Pid)
                {
                    line = i;
                    break;
                }
            }
            return line;
        }
        public List<Cart> GetCart()
        {
            return (List<Cart>)HttpContext.Current.Session["Cart"];
        }

        public List<Cart> DeleteCart(int Pid)
        {
            List<Cart> cart = GetCart();
            if (cart != null)
            {
                for (int i = 0; i < cart.Count; i++)
                {
                    if (int.Parse(cart[i].ProductId.ToString()) == Pid)
                    {
                        cart.RemoveAt(i);
                    }
                }
            }
            return cart;
        }
        public List<Cart> UpdateCart(int Pid, int Quantity)
        {
            List<Cart> cart = GetCart();
            if (Quantity < 1)
            {
                Quantity = 1;
            }
            if (cart != null)
            {
                for (int i = 0; i < cart.Count; i++)
                {
                    if (int.Parse(cart[i].ProductId.ToString()) == Pid)
                    {
                        cart[i].Quantity = Quantity;
                    }
                }
            }
            return cart;
        }
        public bool ClearCart()
        {
            try
            {
                if (HttpContext.Current.Session["Cart"] != null)
                {
                    HttpContext.Current.Session["Cart"] = null;
                }
                else
                {
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public decimal GetTotalPrice()
        {
            List<Cart> cart = GetCart();
            decimal? total = 0;
            if (cart != null)
            {
                total = cart.Select(c => c.Price * c.Quantity).Sum();
            }
            return total ?? decimal.Zero;
        }
        public int CountCart()
        {
            List<Cart> cart = GetCart();
            int count = 0;
            if (cart != null)
            {
                count = cart.Count();
            }
            return count;
        }
    }
}